﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;

namespace Unsk {
	//Player class. each player that join the game will have these attributes.
	public class Player : BasePlayer {
	}

	[RoomType("GameRoom")]
	public class GameCode : Game<Player> {
        //PROTOCOLE
        private const String PROTOCOLE_PICKED_PLANE = "ppp";

		// This method is called when an instance of your the game is created
		public override void GameStarted() {
			// anything you write to the Console will show up in the 
			// output window of the development server
			Console.WriteLine("Game is started");

           // if(this.RoomData.

            AddTimer(delegate {
				// This will cause the GenerateDebugImage() method to be called
				// so you can draw a grapical version of the game state.
				RefreshDebugView(); 
			}, 250);
		}

		// This method is called when the last player leaves the room, and it's closed down.
		public override void GameClosed() {
			Console.WriteLine("RoomId: " + RoomId);
		}

		// This method is called whenever a player joins the game
		public override void UserJoined(Player player) {
			
			//Send info about all already connected users to the newly joined users chat
			Message m = Message.Create("ChatInit");
			m.Add(player.Id);
            

			foreach(Player p in Players) {
				m.Add(p.Id, p.ConnectUserId);
			}

			player.Send(m);

			//Informs other users chats that a new user just joined.
			Broadcast("ChatJoin", player.Id, player.ConnectUserId);
		}

		// This method is called when a player leaves the game
		public override void UserLeft(Player player) {
			//Tell the chat that the player left.
			Broadcast("ChatLeft", player.Id);
		}

		// This method is called when a player sends a message into the server code
		public override void GotMessage(Player player, Message message) {
			switch(message.Type) {
				case PROTOCOLE_PICKED_PLANE: {

						Broadcast("start", player.Id, player.ConnectUserId, message.GetInt(0), message.GetInt(1));
						break;
					}
				case "stop": {
						Broadcast("stop", player.Id);
						break;
					}
				case "move": {
						Broadcast("move", player.Id, message.GetInt(0), message.GetInt(1));
						break;
					}
				case "ChatMessage":{
						Broadcast("ChatMessage", player.Id, message.GetString(0));
						break;
					}
			}
		}


        Point debugPoint;
        public override System.Drawing.Image GenerateDebugImage()
        {
            // we'll just draw 400 by 400 pixels image with the current time, but you can
            // use this to visualize just about anything.
            var image = new Bitmap(640, 480);
            using (var g = Graphics.FromImage(image))
            {
                // fill the background
                g.FillRectangle(Brushes.White, 0, 0, image.Width, image.Height);

                // draw the current time
                g.DrawString(DateTime.Now.ToString(), new Font("Verdana", 20F), Brushes.Orange, 10, 10);

                // draw a dot based on the DebugPoint variable
                g.FillRectangle(Brushes.Red, debugPoint.X, debugPoint.Y, 5, 5);
            }
            return image;
        }
        [DebugAction("Set Debug Point", DebugAction.Icon.Green)]
        public void SetDebugPoint(int x, int y)
        {
            debugPoint = new Point(x, y);
        }
	}
}